<?php
namespace Admin\Controller;
use Think\Controller;
class HoteltestController  extends IndexController {
    public function lists(){
        $type= I('request.type');
        if($type==1){
             $data['cn_name']=I('post.cn_name');
             $data['en_name']=I('post.en_name');
             $cid= I("post.country");
             $country=D("Country");
             $countrys=$country->where('countryid='.$cid)->field("countryname_cn")->find();
             $data['country'] =$countrys['countryname_cn'];
             $data['addr']=I('post.addr');
             $data['abstract']=I('post.abstract');
             $data['level']=I('post.level');
             $data['link']=I('post.link');
             $data['lat']=I('post.lat');
             $data['lng']=I('post.lng');
             $hotel=D('Hotel');

            //=================处理多图片上传==================
          header("content-type:text/html;charset=utf-8");
          $file=$_FILES;
            $newFile=array();
            foreach($file as $key=>$value){
                $num = count($value['name']);
                for($i=0;$i<$num;$i++){
                    $newFile[$i]['name']= $value['name'][$i];
                    $newFile[$i]['type']= $value['type'][$i];
                    $newFile[$i]['tmp_name']=$value['tmp_name'][$i];
                    $newFile[$i]['error']= $value['error'][$i];
                    $newFile[$i]['size']= $value['size'][$i];
                }
            }
            header("content-type:text/html;charset=utf-8");
            $upload = new \Think\Upload(); // 实例化上传类
            $upload->maxSize = 1024*1024*10 ;// 设置附件上传大小 (-1) 是不限值大小
            $upload->allowExts = array(
                'jpg', 'gif', 'png', 'jpeg'
            );// 设置附件上传类型
            $upload->rootPath="./image_hotel/";
//            $upload->savePath = "./Hotel";// 设置附件上传目录
            $upload->replace = true; //存在同名文件是否是覆盖
            // 是否使用子目录保存上传文件
            $upload->autoSub = true;
            $info   =   $upload->upload($newFile);
            if(!$info){
                $data['imgs']="";
            }else{
            //拼接上传的图片路径用于上传到服务器
                $path='';
                for($i=0;$i<=count($info)-1;$i++){
                    $path.=ltrim($info[$i]['savepath'],'./').$info[$i]['savename'].',';
                    $data['imgs']=$path;
                }
            }
            $res=$hotel->add($data);
            //记录兼职增加
            $log=D("Log");
             $opt['name']=$_SESSION['name'];
             $opt['action']='add';
             $opt['model']='Hotel';
             $opt['create_time']=time();
             $opt['log']='员工号为'.$_SESSION['name'].'对酒店模块执行了增加操作,增加了id为'.$res;
             $log->add($opt);
            
            $this->redirect("Hotel/lists");
            
    }elseif($type==2) {
            $this->actionUpdate();
            $this->redirect("Hotel/lists");
        }
        $this->display();
    }
    
    
public function get_test(){
        $hotel= D("Hotel");
        $country=D("Country");
        if(I("request.iDisplayStart")==''){
            
            $start=0;
        }else{
            $start=I("request.iDisplayStart");
        }
        $result=$hotel->field("id,cn_name,en_name,imgs,level,country,addr,link,abstract,lat")->limit($start,I("request.iDisplayLength"))->select();
        if(!$result) {
            echo json_encode(0);exit;
        }
                
                $ret = array(  
                    "sEcho" => intval($_GET['sEcho']),  
                    "iTotalDisplayRecords" => $hotel->count(),  
                    "aaData" => $result  
                );  

        echo json_encode($ret);exit;
    }

//=======================获得酒店列表===================================
    public function get_hotel_list(){
        $hotel= D("Hotel");
        $country=D("Country");
        $result=$hotel->field("id,cn_name,en_name,imgs,level,country,addr,link,abstract,lat,lng")->select();
        if(!$result) {
            echo json_encode(0);exit;
        }

        echo json_encode($result);exit;
    }

    //====================增加酒店的display()模板==============================
    public function add(){
        $this->display();
    }
    //获取国家列表
    public function country_list(){
        $Country=D("Country");
        $r=$Country->select();
        if(!$r){
            echo json_encode(0);exit;
        }
        echo json_encode($r);exit;
    }
    //获取列表
    public function city_list(){
        $cid=I("request.cid");
        $State=D("State");
        $r=$State->where('cid='.$cid)->select();
        if(!$r){
            echo json_encode(0);exit;
        }
        echo json_encode($r);exit;
    }
    //获取医院列表
    public function hospital_list(){
        $zid=I("request.zid");
        $hospital=D("Hospital");
        $result=$hospital->where('zid='.$zid)->select();

        if(!$result){
            echo json_encode(0);
        }
        echo json_encode($result);
    }


    //====================修改酒店==========================
    public function update(){
        $id=I("request.id");
        setcookie("uid", $id, time()+3600);
        $this->display();
    }
    public function update_list(){
        $id=$_COOKIE["uid"];
        $hotel=D("Hotel");
        $r=$hotel->where('id='.$id)->field('cn_name,en_name,imgs,level,country,addr,link,abstract,lat,lng')->find();
        if(!$r){
            echo json_encode(0);exit;
        }

        echo json_encode($r);exit;
    }
   //==============================为了实现图片预览&修改（&删除）&增加的方法================================================
    public function update_imgs()
    {
        $id = $_COOKIE['uid'];
        $Hotel = D('Hotel');
        $type = I("request.type");
        if ($type == 1) {
            $re = $Hotel->where('id=' . $id)->field('id,imgs')->find();
            //===================分隔图片地址&获取图片的拼接的总数量，==============================
            //==============================加上一个$tpye做判断===================================
            if(!empty($re['imgs'])){
                $arr = explode(',', rtrim($re['imgs'], ","));
                $num = count($arr);
                $result['id'] = $re['id'];
                $result['num'] = $num;

                //========================根据分割图片地址的获取的图片总数&把图片分配到图片数组=====================
                for ($i = 0; $i <= $num - 1; $i++) {
                    $result[$i]['imgs'] = $arr[$i];
                 }

                if (!$result) {
                    echo json_encode(0);
                    exit;
                 }
                echo json_encode($result);
                exit;

            }
            //2为删除
        } elseif ($type == 2) {
            $imgNum = I("request.imgNum");
            $delNum = $imgNum;
            $id = $_COOKIE["uid"];
            $Hotel = D("Hotel");
            $getImage = $Hotel->where('id=' . $id)->field('imgs')->find();
            $arr = $getImage['imgs'];
            $res = explode(',', $arr);
            $repaceString = $res[$delNum];
            $str = str_replace($repaceString . ',', "", $arr);
            $data['imgs'] = $str;
            unlink("./image_hotel/" . $repaceString);
            if (isset($_COOKIE["uid"])) {
                $HotelResult = $Hotel->where("id=" . $_COOKIE["uid"])->save($data);
                $log=D("Log");
                $opt['name']=$_SESSION['name'];
                $opt['action']='del';
                $opt['model']='Hotelimg';
                $opt['create_time']=time();
                $opt['log']='员工号为'.$_SESSION['name'].'对酒店图片模块执行了删除操作,删除了id为'.$HotelResult;
                $log->add($opt);
                if (!$HotelResult) {
                    echo json_encode(0);
                    exit;
                } else {
                    echo json_encode($HotelResult);
                    exit;
                }
            }

        }
    }

//==============================当触发loadList()方法时获取的数据=======================================start
    public function  get_hospital_list(){
        $zid=I("request.zid");
        $hospital=D("Hospital");
        $result=$hospital->where('zid='.$zid)->field("id,name")->select();
        if(!$result){
            echo json_encode(0);exit;
        }else{
            echo json_encode($result);exit;
        }
    }
//=================================================================================================end
//==============================================数据呈现到酒店修改页面时====================================================start
    //数据呈现到酒店修改页面时获取数据库的一个数据
    public function get_city_one(){
        $hid=I("request.hid");
        $hospital= D('Hospital');
        $stateId= $hospital->where('id='.$hid)->field('zid')->find();
        $zid=$stateId['zid'];
        $state=D('State');
        $result=$state->where('id='.$zid)->field('id,name')->find();
        if(!$result){
            echo json_encode(0);exit;
        }
            echo json_encode($result);exit;
    }

    public function get_hospital_one(){
        $hospital= D('hospital');
        $hid=I("request.hid");
        $result= $hospital->where('id='.$hid)->field('id,name')->find();
        if(!$result){
            echo json_encode(0);exit;
        }
        echo json_encode($result);exit;

    }

    //当确实修改了国家时获得地区信息
    public function get_city_list(){
        $cid=I("request.cid");
        $state=D('state');
        $result= $state->where("cid=".$cid)->select();
        if(!$result){
            echo json_encode(0);exit;
        }
        echo json_encode($result);exit;

    }
    //数据修改时呈现得国家信息
    public function get_country_list(){
        $country=D('country');
        $result= $country->select();
        if(!$result){
            echo json_encode(0);exit;
        }
        echo json_encode($result);exit;
    }
//=================================================================================================end
    // 更新数据
    public function actionUpdate(){
        $Hotel = D('Hotel');
        
        $data['cn_name']=I('post.cn_name');
        $data['en_name']=I('post.en_name');
        $cid= I("post.country");
        $country=D("Country");
        $countrys=$country->where('countryid='.$cid)->field("countryname_cn")->find();
        $data['country'] =$countrys['countryname_cn'];
        $data['addr']=I('post.addr');
        $data['abstract']=I('post.abstract');
        $data['level']=I('post.level');
        $data['link']=I('post.link');
        $data['lat']=I('post.lat');
        $data['lng']=I('post.lng');
        if ($_FILES['imgs']['name'][0] != null) {
            $file = $_FILES;
            $newFile = array();
        foreach ($file as $key => $value) {
            $num = count($value['name']);
            for ($i = 0; $i < $num; $i++) {
                $newFile[$i]['name'] = $value['name'][$i];
                $newFile[$i]['type'] = $value['type'][$i];
                $newFile[$i]['tmp_name'] = $value['tmp_name'][$i];
                $newFile[$i]['error'] = $value['error'][$i];
                $newFile[$i]['size'] = $value['size'][$i];
            }
        }
        header("content-type:text/html;charset=utf-8");

        $upload = new \Think\Upload(); // 实例化上传类
        $upload->maxSize = 1024 * 1024 * 10;// 设置附件上传大小 (-1) 是不限值大小
        $upload->allowExts = array(
            'jpg', 'gif', 'png', 'jpeg'
        );// 设置附件上传类型
        $upload->rootPath = "./image_hotel/";
//        $upload->savePath = "./Hotel";// 设置附件上传目录
        $upload->replace = true; //存在同名文件是否是覆盖
        // 是否使用子目录保存上传文件
        $upload->autoSub = true;
        $info = $upload->upload($newFile);
        if (!$info) {
            $data['imgs'] = "";
        } else {
            //拼接上传的图片路径用于上传到服务器
            $path = '';
            for ($i = 0; $i <= count($info) - 1; $i++) {
                $path .= ltrim($info[$i]['savepath'], './') . $info[$i]['savename'] . ',';
            }
            if (isset($_COOKIE["uid"])) {
                $getData = $Hotel->where("id=" . $_COOKIE["uid"])->getField('imgs');
            }

            if ($path != null) {
                $data['imgs'] = $path . $getData;
            } else {
                $data['imgs'] = $getData;
            }
        }
    }
        if(isset($_COOKIE["uid"])){
            $Hotel->where("id=".$_COOKIE["uid"])->save($data);
             $log=D("Log");
             $opt['name']=$_SESSION['name'];
             $opt['action']='update';
             $opt['model']='Hotel';
             $opt['create_time']=time();
             $opt['log']='员工号为'.$_SESSION['name'].'对酒店模块执行了修改操作,修改了id为'.$_COOKIE["uid"];
             $log->add($opt);
       }


  }
    //修除数据
    public function del(){
        $id=I("request.id");
        $hotel=D("Hotel");
            $result=$hotel->where('id='.$id)->find();
        //=============================删除多图片的地址=================================
        if($result['imgs']!=null){
            $strResult = rtrim($result['imgs'], ',');
            $arrResult = explode(',', $strResult);
            $count=count($arrResult);
            for($i=0;$i<$count;$i++){
                unlink("./image_hotel/" .  $arrResult[$i] );
            }
        }
        $result=$hotel->delete($id);
        
        
        if(!$result){
            echo json_encode(0);exit;
        }
            $log=D("Log");
             $opt['name']=$_SESSION['name'];
             $opt['action']='del';
             $opt['model']='Hotel';
             $opt['create_time']=time();
             $opt['log']='员工号为'.$_SESSION['name'].'对酒店模块执行了删除操作,删除了id为'.$id;
             $log->add($opt);
        echo json_encode($result);exit;
    }


}